package api

import (
	restfulspec "github.com/emicklei/go-restful-openapi"
	"github.com/emicklei/go-restful/v3"
	"github.com/infraboard/mcube/app"
	"github.com/infraboard/mcube/http/response"
	"github.com/infraboard/mcube/logger"
	"github.com/infraboard/mcube/logger/zap"
	"kubernetes-manage/apps/cluster"

	"kubernetes-manage/apps/book"
)

var (
	h = &handler{}
)

type handler struct {
	service cluster.ServiceServer
	log     logger.Logger
}

func (h *handler) Config() error {
	h.log = zap.L().Named(book.AppName)
	h.service = app.GetGrpcApp(cluster.AppName).(cluster.ServiceServer)
	return nil
}

func (h *handler) Name() string {
	return cluster.AppName
}

func (h *handler) Version() string {
	return "v1"
}

func (h *handler) Registry(ws *restful.WebService) {
	tags := []string{"cluster"}

	ws.Route(ws.POST("/create").To(h.CreateCluster).
		Doc("create a book").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Reads(cluster.CreateClusterRequest{}).
		Writes(response.NewData(cluster.ClusterInfo{})))

	ws.Route(ws.GET("/query").To(h.QueryCluster).
		Doc("get all books").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Metadata("action", "list").
		Reads(cluster.CreateClusterRequest{}).
		Writes(response.NewData(cluster.ClusterSet{})).
		Returns(200, "OK", cluster.ClusterSet{}))
}

func init() {
	app.RegistryRESTfulApp(h)
}
